#include "scheduler/if_then_else_scheduler.h"
#include "../policy/policy_factory.h"
#include "../policy/behavior_tree/behavior_tree_if_then_else_policy.h"

namespace scheduler
{
IfThenElseScheduler::IfThenElseScheduler(const std::string &name)
    : AbstractScheduler(name)
{
    schedule_policy_ = policy::PolicyFactory<policy::BehaviorTreeIfThenElsePolicy>::createPolicy(name + "_policy");
}
IfThenElseScheduler::~IfThenElseScheduler() = default;

bool IfThenElseScheduler::setConditionTask(const task::AbstractTaskConstPtr &task)
{
    if (task->getTaskType() != task::TaskType::SYNC_REQUEST_TASK)
    {
        EASE_ERROR("if then else scheduler condition must be request task");
        throw std::runtime_error("if then else scheduler condition must be request task");
    }
    auto policy = dynamic_cast<policy::BehaviorTreeIfThenElsePolicy *>(schedule_policy_.get());
    if (nullptr == policy)
    {
        throw std::runtime_error("if then else scheduler policy case fail");
    }
    return policy->setConditionTask(task);
}
bool IfThenElseScheduler::setConditionScheduler(const AbstractSchedulerConstPtr &scheduler)
{
    auto policy = dynamic_cast<policy::BehaviorTreeIfThenElsePolicy *>(schedule_policy_.get());
    if (nullptr == policy)
    {
        throw std::runtime_error("if then else scheduler policy case fail");
    }
    scheduler_vec_.push_back(scheduler);
    return policy->setConditionPolicy(scheduler->getTaskPolicy());
}
bool IfThenElseScheduler::setElseTask(const task::AbstractTaskConstPtr &task)
{
    auto policy = dynamic_cast<policy::BehaviorTreeIfThenElsePolicy *>(schedule_policy_.get());
    if (nullptr == policy)
    {
        throw std::runtime_error("if then else scheduler policy case fail");
    }
    return policy->setElseTask(task);
}
bool IfThenElseScheduler::setElseScheduler(const AbstractSchedulerConstPtr &scheduler)
{
    auto policy = dynamic_cast<policy::BehaviorTreeIfThenElsePolicy *>(schedule_policy_.get());
    if (nullptr == policy)
    {
        throw std::runtime_error("if then else scheduler policy case fail");
    }
    scheduler_vec_.push_back(scheduler);
    return policy->setElsePolicy(scheduler->getTaskPolicy());
}
bool IfThenElseScheduler::setThenTask(const task::AbstractTaskConstPtr &task)
{
    auto policy = dynamic_cast<policy::BehaviorTreeIfThenElsePolicy *>(schedule_policy_.get());
    if (nullptr == policy)
    {
        throw std::runtime_error("if then else scheduler policy case fail");
    }
    return policy->setThenTask(task);
}
bool IfThenElseScheduler::setThenScheduler(const AbstractSchedulerConstPtr &scheduler)
{
    auto policy = dynamic_cast<policy::BehaviorTreeIfThenElsePolicy *>(schedule_policy_.get());
    if (nullptr == policy)
    {
        throw std::runtime_error("if then else scheduler policy case fail");
    }
    scheduler_vec_.push_back(scheduler);
    return policy->setThenPolicy(scheduler->getTaskPolicy());
}
bool IfThenElseScheduler::appendTask(const task::AbstractTaskConstPtr &task)
{
    EASE_ERROR("if else scheduler cannot append task");
    throw std::runtime_error("if else scheduler cannot append task");
}
bool IfThenElseScheduler::appendScheduler(const AbstractSchedulerConstPtr &scheduler)
{
    EASE_ERROR("if else scheduler cannot append scheduler");
    throw std::runtime_error("if else scheduler cannot append scheduler");
}
bool IfThenElseScheduler::setTask(const task::AbstractTaskConstPtr &task)
{
    EASE_ERROR("if else scheduler cannot set task");
    throw std::runtime_error("if else scheduler cannot set task");
}
bool IfThenElseScheduler::setScheduler(const AbstractSchedulerConstPtr &scheduler)
{
    EASE_ERROR("if else scheduler cannot set scheduler");
    throw std::runtime_error("if else scheduler cannot set scheduler");
}
}
